package com.xn.app.muying.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xn.app.muying.R;
import com.xn.app.muying.activity.ZhangHuActivity;
import com.xn.app.muying.activity.BuyServiceActivity;
import com.xn.app.muying.activity.MonitorSelectActivity;
import com.xn.app.muying.activity.PayFailActivity;
import com.xn.app.muying.activity.ReadyPayActivity;
import com.xn.app.muying.activity.VIPActivity;
import com.xn.app.muying.util.BaseActivity;
import com.xn.app.muying.util.WeChatPayUtils;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;
    public static int i = -1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payfail);

        api = WXAPIFactory.createWXAPI(this, WeChatPayUtils.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        Log.d(TAG, req.transaction);
    }

    /**
     * 参照微信SDK Sample，
     * 在net.sourceforge.simcpux.wxapi包路径中实现WXPayEntryActivity类(包名或类名不一致会造成无法回调)，
     * 在WXPayEntryActivity类中实现onResp函数，
     * 支付完成后，微信APP会返回到商户APP并回调onResp函数，
     * 开发者需要在该函数中接收通知，判断返回错误码，
     * 如果支付成功则去后台查询支付结果再展示用户实际支付结果。
     * 注意一定不能以客户端返回作为用户支付的结果，应以服务器端的接收的支付通知或查询API返回的结果为准。
     *
     * @param resp
     */
    @Override
    public void onResp(BaseResp resp) {
//        EventBus.getDefault().post(resp);
        //0 成功 展示成功页面
        //-1 错误 可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
        //-2 用户取消 无需处理。发生场景：用户不支付了，点击取消，返回APP。
        if (resp.errCode == 0) {//支付成功
            sendBroadcast(new Intent("bei_er").putExtra("bei_er", 0));
            if (ReadyPayActivity.sign.equals("vip")) {
                if (ReadyPayActivity.readyPayActivity != null) {
                    ReadyPayActivity.readyPayActivity.finish();
                }
                if (ZhangHuActivity.zhangHuActivity != null) {
                    ZhangHuActivity.zhangHuActivity.finish();
                }
                if (VIPActivity.viPActivity != null) {
                    VIPActivity.viPActivity.finish();
                }
                finish();
            } else {
                if (MonitorSelectActivity.monitorSelectActivity!=null){
                    MonitorSelectActivity.monitorSelectActivity.finish();
                }
                Intent intent = new Intent(WXPayEntryActivity.this, MonitorSelectActivity.class);
                intent.putExtra("success",BuyServiceActivity.list.get(BuyServiceActivity.mIndex).getPaySuccessDescription());
                startActivity(intent);
                i=0;
                if (ReadyPayActivity.readyPayActivity != null) {
                    ReadyPayActivity.readyPayActivity.finish();
                }
                if (BuyServiceActivity.buyServiceActivity != null) {
                    BuyServiceActivity.buyServiceActivity.finish();
                }
                finish();
            }
        } else {//支付失败
            if (ReadyPayActivity.sign.equals("vip")) {
                Intent intent = new Intent(WXPayEntryActivity.this, PayFailActivity.class);
                intent.putExtra("type", "vip");
                startActivity(intent);
                finish();
            } else {
                Intent intent = new Intent(WXPayEntryActivity.this, PayFailActivity.class);
                intent.putExtra("type", "monitor");
                startActivity(intent);
                if (ReadyPayActivity.readyPayActivity != null) {
                    ReadyPayActivity.readyPayActivity.finish();
                }
                if (BuyServiceActivity.buyServiceActivity != null) {
                    BuyServiceActivity.buyServiceActivity.finish();
                }
                finish();
            }
        }
//        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle("提示");
//            builder.setMessage(String.format("微信支付结果：%s", resp.errCode + ""));
//            builder.show();
//        }
    }
}